import logging

from django.core.mail import EmailMultiAlternatives, get_connection

logger = logging.getLogger('django')


def send_mail(subject, message, receiver, from_email='noreply@knownsec.com', file_path=None, connection=None):
    if not connection:
        connection = get_connection(
            username=None,
            password=None,
            fail_silently=False,
        )

    recipient_list = [receiver] if not isinstance(receiver, list) else receiver
    if isinstance(file_path, list):
        file_path_list = file_path
    else:
        if file_path:
            file_path_list = [file_path]
        else:
            file_path_list = []

    logger.info("send attendance email to :{}".format(recipient_list))
    mail = EmailMultiAlternatives(subject, message, from_email, recipient_list, connection=connection)

    for attach_file in file_path_list:
        mail.attach_file(attach_file)
    return mail.send()
